﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using XJLSC.QM;
using FastReport.Utils;
using System.IO;
using FastReport;
using XJLSC.PM;

namespace SMALLERP.PM
{
    public partial class CJBGD : Form
    {
        DataBase db = new DataBase();
        private SqlDataReader sdr1;
        SCKC f;
        DDKC g;

        private Report FReport;
        private DataSet FDataSet;
        private DataSet FDataSet1;
        private string FReportsFolder;
        private bool FReportRunning;
        public CJBGD()
        {
            InitializeComponent();
        }

        public delegate void CheckBoxClickedHandler(bool state);
        public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
        {
            bool _bChecked;
            public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
            {
                _bChecked = bChecked;
            }
            public bool Checked
            {
                get { return _bChecked; }
            }
        }
        class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
        {
            Point checkBoxLocation;
            Size checkBoxSize;
            bool _checked = false;
            Point _cellLocation = new Point();
            System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
            public event CheckBoxClickedHandler OnCheckBoxClicked;

            public DatagridViewCheckBoxHeaderCell()
            {
            }

            protected override void Paint(System.Drawing.Graphics graphics,
                System.Drawing.Rectangle clipBounds,
                System.Drawing.Rectangle cellBounds,
                int rowIndex,
                DataGridViewElementStates dataGridViewElementState,
                object value,
                object formattedValue,
                string errorText,
                DataGridViewCellStyle cellStyle,
                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                    dataGridViewElementState, value,
                    formattedValue, errorText, cellStyle,
                    advancedBorderStyle, paintParts);
                Point p = new Point();
                Size s = CheckBoxRenderer.GetGlyphSize(graphics,
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
                p.X = cellBounds.Location.X +
                    (cellBounds.Width / 2) - (s.Width / 2);
                p.Y = cellBounds.Location.Y +
                    (cellBounds.Height / 2) - (s.Height / 2);
                _cellLocation = cellBounds.Location;
                checkBoxLocation = p;
                checkBoxSize = s;
                if (_checked)
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.CheckedNormal;
                else
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.UncheckedNormal;
                CheckBoxRenderer.DrawCheckBox
                (graphics, checkBoxLocation, _cbState);
            }

            protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
            {
                Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
                if (p.X >= checkBoxLocation.X && p.X <=
                    checkBoxLocation.X + checkBoxSize.Width
                && p.Y >= checkBoxLocation.Y && p.Y <=
                    checkBoxLocation.Y + checkBoxSize.Height)
                {
                    _checked = !_checked;
                    if (OnCheckBoxClicked != null)
                    {
                        OnCheckBoxClicked(_checked);
                        this.DataGridView.InvalidateCell(this);
                    }

                }
                base.OnMouseClick(e);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            try
            {
                string strSql = "";
                if (checkBox1.CheckState == CheckState.Unchecked)
                {
                    strSql = "SELECT RTRIM(ISNULL(MW002,'****')) 工艺名称,TY001 单别,TY002 单号,RTRIM(TY003) 日期,CAST(TY004 AS REAL) 数量,TA027 订单号,MB002 品名,MB003 规格,MB001 品号,MF004 工艺,TA001,TA002,ISNULL((SELECT TOP 1 C.EMPLOYEE_CODE FROM ISSUE_RECEIPT_D A LEFT JOIN ISSUE_RECEIPT B ON A.ISSUE_RECEIPT_ID=B.ISSUE_RECEIPT_ID LEFT JOIN MO D ON D.MO_ID=A.MO_ID LEFT JOIN EMPLOYEE C ON B.Owner_Emp=C.EMPLOYEE_ID WHERE ISNULL(C.EMPLOYEE_CODE,'')<>'' AND D.DOC_NO=TA001+'-'+TA002),'') 员工编号,'' 员工姓名,TA083 工位 FROM MOCTY LEFT JOIN MOCTA ON TY001=TA001 AND TY002=TA002 LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MF001=TA006 AND MF003=TY019 LEFT JOIN CMSMW ON MF004=MW001 WHERE (TY018='' OR TY018 IS NULL) AND TY003=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA033 LIKE '%" + textBox3.Text + "%' OR MOCTA.TA002 LIKE '%" + textBox3.Text + "%') AND TA011 <> 'Y' AND TA011 <> 'y' AND TY004>0 AND (ISNULL(MW002,'') LIKE '%" + textBox14.Text + "%' OR TY019='" + textBox14.Text + "')  ORDER BY TY009";
                   // textBox6.Text = strSql;
                    try
                    {
                        DataTable dt = this.dataGridView1.DataSource as DataTable;
                        dataGridView1.DataSource = "";
                        dataGridView1.Refresh();
                        dt = db.GetDataSet1(strSql, "MOCTY").Tables["MOCTY"];
                        dataGridView1.DataSource = dt;

                        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                        this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                        this.dataGridView1.AllowUserToAddRows = false;

                        this.dataGridView1.Columns["工艺"].Visible = false;
                        this.dataGridView1.Columns["员工姓名"].ReadOnly = true;
                        this.dataGridView1.Columns["订单号"].Visible = true;
                        this.dataGridView1.Columns["TA001"].Visible = false;
                        this.dataGridView1.Columns["TA002"].Visible = false;

                        for(int i=0;i<=dataGridView1.Rows.Count-1;i++)
                        {
                            String ygbh = (dataGridView1.Rows[i].Cells["员工编号"].Value == null) ? "" : dataGridView1.Rows[i].Cells["员工编号"].Value.ToString();
                            if(ygbh != "")
                            {
                                strSql = "SELECT MV002 FROM CMSMV WHERE MV001='" + ygbh + "'";
                                try
                                {
                                    sdr1 = db.GetDataReader1(strSql);

                                    while (sdr1.Read())
                                    {
                                        this.dataGridView1.Rows[i].Cells["员工姓名"].Value = sdr1["MV002"].ToString();

                                    }
                                }

                                catch 
                                {
                                   
                                }
                                finally
                                {
                                    sdr1.Close();
                                }
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        //throw ex;
                    }
                }
                else if (checkBox1.CheckState == CheckState.Checked)
                {
                    //strSql = "SELECT V_GXXL.TY009 as 线别,V_GXXL.TY001 as 工单单别,V_GXXL.TY002 as 工单单号,MOCTA.TA034 产品品名,V_GXXL.TB003 材料品号,INVMB1.UDF01 材料老编码,INVMB1.MB002 材料品名,INVMB1.MB003 材料规格,CEILING((CASE WHEN ISNULL(V_GXLL.TE005,0)>0 THEN V_GXXL.XL-ISNULL(V_GXLL.TE005,0) ELSE V_GXXL.XL END)) as 缺领数,CEILING(CASE WHEN ISNULL(V_INVMC_MOCTE.KYKC,0)-ISNULL(V_DL.TE005,0)<0 THEN 0 ELSE ISNULL(V_INVMC_MOCTE.KYKC,0)-ISNULL(V_DL.TE005,0) END) 可用库存,CEILING(V_GXXL.TY004) 排产数,CEILING(V_GXXL.XL) 需领量,CEILING(ISNULL(V_GXLL.TE005,0)) 已领量,INVMB1.MB017 仓库,CEILING(ISNULL(INVMC.MC007,0)) 仓库库存,V_GXXL.TY019 工艺,RTRIM(ISNULL(MW002,'****')) 工艺名称  ";
                    //strSql = strSql + "FROM V_GXXL  Left JOIN V_GXLL On V_GXXL.TY001=V_GXLL.TE011 and V_GXXL.TY002=V_GXLL.TE012 and V_GXXL.TB003=V_GXLL.TE004 and V_GXXL.TY019=V_GXLL.TE009 and V_GXXL.TY009=V_GXLL.TC005 Left JOIN INVMB INVMB1 On V_GXXL.TB003=INVMB1.MB001 Left JOIN MOCTA On V_GXXL.TY001=MOCTA.TA001 and V_GXXL.TY002=MOCTA.TA002 LEFT JOIN INVMB INVMB2 ON INVMB2.MB001=TA006 LEFT JOIN V_INVMC_MOCTE ON PH=V_GXXL.TB003 AND CK=INVMB1.MB017 LEFT JOIN INVMC ON INVMC.MC001=V_GXXL.TB003 AND INVMC.MC002=INVMB1.MB017 LEFT JOIN V_DL ON V_GXXL.TB003=V_DL.TE004 AND INVMB1.MB017=V_DL.TE008 LEFT JOIN CMSMW ON V_GXXL.TY019=MW001 WHERE INVMB1.MB042='1' AND  (MOCTA.TA021 LIKE '%" + textBox1.Text + "%' OR '" + textBox1.Text + "'='') AND  (MW002= '" + comboBox1.Text + "' OR V_GXXL.TY019='" + comboBox1.Text + "' OR '" + comboBox1.Text + "'='') AND (INVMB1.UDF01 LIKE '%" + textBox4.Text + "%' OR INVMB1.MB002 LIKE '%" + textBox4.Text + "%' OR '" + textBox4.Text + "'='') AND (INVMB2.UDF01 LIKE '%" + textBox3.Text + "%' OR '" + textBox3.Text + "'='') AND ((MOCTA.TA011 <> 'Y') AND (MOCTA.TA011 <> 'y') AND (V_GXXL.TY018 <> '') AND (V_GXXL.XL > 0)) AND V_GXXL.XL-ISNULL(V_GXLL.TE005,0)>0   ORDER BY V_GXXL.TY001 asc,V_GXXL.TY002 asc,V_GXXL.TY009 asc";

                    if (checkBox2.CheckState == CheckState.Unchecked)
                    {
                    strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 日期,CAST(TA015 AS REAL) 数量,CAST(TA017 AS REAL) 已入库数量,TA033 订单号,MB002 品名,MB003 规格,MB001 品号,ISNULL((SELECT TOP 1 C.EMPLOYEE_CODE FROM ISSUE_RECEIPT_D A LEFT JOIN ISSUE_RECEIPT B ON A.ISSUE_RECEIPT_ID=B.ISSUE_RECEIPT_ID LEFT JOIN MO D ON D.MO_ID=A.MO_ID LEFT JOIN EMPLOYEE C ON B.Owner_Emp=C.EMPLOYEE_ID WHERE ISNULL(C.EMPLOYEE_CODE,'')<>'' AND D.DOC_NO=TA001+'-'+TA002),'') 员工编号,'' 员工姓名,TA083 工位,'****' 工艺 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA033 LIKE '%" + textBox3.Text + "%' OR MOCTA.TA002 LIKE '%" + textBox3.Text + "%') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA011 <> 'Y' AND TA011 <> 'y' AND EXISTS(SELECT * FROM MOCTB LEFT JOIN INVMB ON TB003=MB001 WHERE TA001=TB001 AND TA002=TB002 AND MB042<>'3' AND TB004-TB005>0) ORDER BY TA006";
                    }
                    else
                    {
                     strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 日期,CAST(TA015 AS REAL) 数量,CAST(TA017 AS REAL) 已入库数量,TA033 订单号,MB002 品名,MB003 规格,MB001 品号,ISNULL((SELECT TOP 1 C.EMPLOYEE_CODE FROM ISSUE_RECEIPT_D A LEFT JOIN ISSUE_RECEIPT B ON A.ISSUE_RECEIPT_ID=B.ISSUE_RECEIPT_ID LEFT JOIN MO D ON D.MO_ID=A.MO_ID LEFT JOIN EMPLOYEE C ON B.Owner_Emp=C.EMPLOYEE_ID WHERE ISNULL(C.EMPLOYEE_CODE,'')<>'' AND D.DOC_NO=TA001+'-'+TA002),'') 员工编号,'' 员工姓名,TA083 工位,'****' 工艺 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA033 LIKE '%" + textBox3.Text + "%' OR MOCTA.TA002 LIKE '%" + textBox3.Text + "%') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA011 <> 'Y' AND TA011 <> 'y' ORDER BY TA006";
                    }
                    //textBox6.Text = strSql;
                    try
                    {
                        DataTable dt = this.dataGridView1.DataSource as DataTable;

                        if (this.dataGridView1.Rows.Count > 1)
                        {
                            dt.Rows.Clear();
                            this.dataGridView1.DataSource = dt;
                        }
                        dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                        dataGridView1.DataSource = dt;

                        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                        this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                        this.dataGridView1.AllowUserToAddRows = false;

                        //this.dataGridView1.Columns["工艺"].Visible = false;

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        //throw ex;
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
            gridControl1.DataSource = null;
        }

        private void CJBGD_Load(object sender, EventArgs e)
        {
          
            textBox2.Text = "5610";
            //textBox4.Enabled = false;

            DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
            DatagridViewCheckBoxHeaderCell bbHeader = new DatagridViewCheckBoxHeaderCell();
            colCB.HeaderCell = bbHeader;
            colCB.Name = "colCB";
            colCB.HeaderText = "";
            dataGridView1.Columns.Insert(0, colCB);
            bbHeader.OnCheckBoxClicked +=
                new CheckBoxClickedHandler(abHeader_OnCheckBoxClicked);

            DataGridViewCheckBoxColumn colCC = new DataGridViewCheckBoxColumn();
            DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
            colCC.HeaderCell = cbHeader;
            colCC.Name = "colCC";
            colCC.HeaderText = "";
            cbHeader.OnCheckBoxClicked +=
                new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);

            string thisFolder = Config.ApplicationFolder;
            DirectoryInfo theFolder = new DirectoryInfo(thisFolder + @"Reports\领料单\");
            FileInfo[] fileInfo = theFolder.GetFiles();
            foreach (FileInfo NextFile in fileInfo)  //遍历文件
            {
                string gsmc = "";
                //gsmc = System.IO.Path.GetFileNameWithoutExtension(theFolder + NextFile.Name);
                gsmc = System.IO.Path.GetFileNameWithoutExtension(theFolder + NextFile.Name);
                comboBox1.Items.Add(gsmc);
                if(comboBox1.Text=="")
                {
                    comboBox1.Text = gsmc;
                }
            }
        }

        private void abHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态
            dataGridView1.EndEdit();
            dataGridView1.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void cbHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态
            //dataGridView2.EndEdit();
            //dataGridView2.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(textBox1.Text=="")
            {
                MessageBox.Show("工作中心不可为空！","系统提示");
                return;
            }
            String ygbh = "";
            String gwbh = "";
            if (dataGridView1.Rows.Count >=1 && dataGridView1.Columns.Count>1)
            {
                 ygbh = (dataGridView1.CurrentRow.Cells["员工编号"].Value == null) ? "" : dataGridView1.CurrentRow.Cells["员工编号"].Value.ToString();
                 gwbh = (dataGridView1.CurrentRow.Cells["工位"].Value == null) ? "" : dataGridView1.CurrentRow.Cells["工位"].Value.ToString();
                if (ygbh == "")
                {
                    MessageBox.Show("员工编号不可为空！", "系统提示");
                    return;
                }

                if (gwbh == "")
                {
                    MessageBox.Show("工位编号不可为空！", "系统提示");
                    return;
                }
            }
            string strSql = null;
            //if (checkBox1.CheckState == CheckState.Unchecked)
            //{

            //        foreach (DataGridViewRow dr in this.dataGridView1.Rows)
            //        {
            //            if (Convert.ToBoolean(dr.Cells["check"].Value))
            //            {
            //                strSql = "EXEC [dbo].[sp_lld] '" + dr.Cells["单别"].Value + "','" + dr.Cells["单号"].Value + "','" + dr.Cells["日期"].Value + "','" + dr.Cells["线别"].Value + "'," + dr.Cells["数量"].Value + ",'" + PropertyClass.OperatorCode + "','" + textBox2.Text + "','" + dr.Cells["工艺"].Value + "'";
            //                try
            //                {
            //                    if (db.ExecDataBySql1(strSql) > 0)
            //                    {

            //                    }
            //                    else
            //                    {
            //                        MessageBox.Show("生成领料单失败，请检查", "软件提示");
            //                        return;
            //                    }
            //                }
            //                catch (Exception ex)
            //                {
            //                    MessageBox.Show(ex.Message, "软件提示");
            //                    throw ex;
            //                }
            //            }

            //        }


            //    MessageBox.Show("领料单生成成功！", "软件提示");
            //    button2.PerformClick();
            //}

            //if (checkBox1.CheckState == CheckState.Checked)
            //{   
            strSql = "DELETE FROM MO_TEMP_LTL WHERE DLH='" + PropertyClass.OperatorCode + "'";
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {
                }
                else
                {
                }
            }
            catch
            {

            }

            if (checkBox3.CheckState == CheckState.Unchecked)
            {

                int[] rows = this.gridView1.GetSelectedRows();
                foreach (int row in rows)
                {
                    String mo_id = this.gridView1.GetRowCellValue(row, "MOID").ToString();
                    String mo_d_id = this.gridView1.GetRowCellValue(row, "MODID").ToString();
                    String source_rtk = this.gridView1.GetRowCellValue(row, "SOURCEIDRTK").ToString();
                    String source_roid = this.gridView1.GetRowCellValue(row, "SOURCEIDROid").ToString();
                    String qty = this.gridView1.GetRowCellValue(row, "本次领料").ToString();
                    String owner = this.gridView1.GetRowCellValue(row, "OwnerOrgROid").ToString();
                    String CK = this.gridView1.GetRowCellValue(row, "WAREHOUSE_ID").ToString();
                    String llry = this.gridView1.GetRowCellValue(row, "领料人员").ToString();
                    if (ygbh == "")
                    {
                        ygbh = llry;
                    }

                    strSql = "INSERT MO_TEMP_LTL SELECT '" + PropertyClass.OperatorCode + "','" + mo_id + "','" + mo_d_id + "','" + source_rtk + "','" + source_roid + "'," + qty + ",'','" + owner + "','" + CK + "'";
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                        }
                        else
                        {

                        }
                    }
                    catch
                    {

                    }
                }
            }
            else
            {
                foreach (DataGridViewRow dr in this.dataGridView1.Rows)
                {
                    if (Convert.ToBoolean(dr.Cells["colCB"].Value))
                    {
                        strSql = "INSERT MO_TEMP_LTL SELECT '" + PropertyClass.OperatorCode + "',MO.MO_ID,MOCTB.MO_D_ID,SOURCE_ID_RTK,SOURCE_ID_ROid,(CASE WHEN (CEILING(TB004)-CEILING(TB005+ISNULL(C.TE005,0)))<CEILING(CASE WHEN TA015=0 THEN 0 ELSE TB004 / TA015 * " + dr.Cells["数量"].Value + " END) THEN (CEILING(TB004)-CEILING(TB005+ISNULL(C.TE005,0))) ELSE CEILING(CASE WHEN TA015=0 THEN 0 ELSE TB004 / TA015 * " + dr.Cells["数量"].Value + " END) END),MO.Owner_Org_ROid,MOCTB.WAREHOUSE_ID FROM MOCTB LEFT JOIN MOCTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN INVMB ON TB003=MB001 LEFT JOIN CMSMC A ON A.MC001=TB009 LEFT JOIN INVMC B ON TB003=B.MC001 AND TB009=B.MC002 LEFT JOIN V_CLDL C ON TB001=C.TE011 AND TB002=C.TE012 AND TB003=C.TE004 LEFT JOIN V_DL D ON D.TE004=TB003 AND D.TE008=TB009 LEFT JOIN CMSMW ON TB006=MW001 LEFT JOIN ITEM ON ITEM_CODE=TB003 LEFT JOIN OPERATION ON TB006=OPERATION_CODE  LEFT JOIN MO ON TB001+'-'+TB002=MO.DOC_NO WHERE  TB001='" + dr.Cells["单别"].Value + "' AND TB002='" + dr.Cells["单号"].Value + "' AND MB042<>'2' AND CEILING(TB004)-CEILING(TB005+ISNULL(C.TE005,0))>0 AND (TB006='" + dr.Cells["工艺"].Value + "' OR '" + dr.Cells["工艺"].Value + "'='****')";
                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {
                            }
                            else
                            {

                            }
                        }
                        catch
                        {

                        }
                    }
                }
            }
        
            strSql = "SELECT DISTINCT WAREHOUSE_ID FROM MO_TEMP_LTL WHERE DLH='" + PropertyClass.OperatorCode + "'";
            try
            {
                sdr1 = db.GetDataReader1(strSql);
                while (sdr1.Read())
                {
                    strSql = "EXEC [SP_MO_TEMP_LTL] '" + PropertyClass.OperatorCode + "','" + textBox2.Text + "','" + sdr1["WAREHOUSE_ID"].ToString() + "','1','" + dateTimePicker1.Value.ToShortDateString() + "','" + ygbh + "','" + gwbh + "'";
                   // textBox3.Text = strSql;
                    try
                    {
                        db = new DataBase();
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                                   
                        }
                        else
                        {

                        }
                    }
                    catch
                    {

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
            finally
            {
                sdr1.Close();
            }

            MessageBox.Show("领料单生成成功！", "软件提示");
            button2.PerformClick();

            //string x1 = textBox2.Text;
            //string x2 = PropertyClass.OperatorCode;
            //string x3 = "";
            //string x4 = "";
            //string x5 = "";
            //string x6 = "";
            //string x7 = "";
            //string x8 = "";
            //JYCX xx = new JYCX(x1, x2, x3, x4, x5, x6, x7, x8);
            //LLDPrint g = new LLDPrint();
            //g.cdssh = new LLDPrint.Cdssh(textBox1_TextChanged);
            //g.Show();

            //if (radioButton1.Checked == true)
            //{
            //strSql = "EXEC [SP_MO_TEMP_LTL] '" + PropertyClass.OperatorCode + "','" + textBox2.Text + "','" + textBox5.Text + "','0','" + dateTimePicker1.Value.ToShortDateString() + "'";

            //}
            //else
            //{
            //    strSql = "EXEC [SP_MO_TEMP_LTL] '" + PropertyClass.OperatorCode + "','" + textBox1.Text + "','" + textBox5.Text + "','0','" + dateTimePicker1.Value.ToShortDateString() + "'";
            //}
            //try
            //{
            //    if (db.ExecDataBySql1(strSql) > 0)
            //    {
            //        //if (radioButton1.Checked == true)
            //        //{
            //        //    MessageBox.Show("退料单生成成功！", "软件提示");
            //        //}
            //        //else
            //        //{
            //            MessageBox.Show("领料单生成成功！", "软件提示");
            //        //}
            //    }
            //    else
            //    {

            //    }
            //}
            //catch
            //{

            //}

            //MessageBox.Show("领料单生成成功！单号" + dh, "软件提示");
            //button2.PerformClick();

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //if (checkBox1.CheckState == CheckState.Checked)
            //{
            //    double kykc = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["可用库存"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["可用库存"].Index].Value);
            //    double qls = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["缺领数"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["缺领数"].Index].Value);
            //    if (kykc >= qls)
            //    {

            //        for (int i = 0; i <= this.dataGridView1.Columns.Count - 1; i++)
            //        {
            //            this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.BackColor = Color.Green;
            //        }
            //    }
            //}
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.CheckState == CheckState.Checked)
            {
                textBox4.Enabled = true;
            }
            if (checkBox1.CheckState == CheckState.Unchecked)
            {
                textBox4.Enabled = false;
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex.Equals(this.dataGridView1.Columns["colCB"].Index))
            {

                if (Convert.ToBoolean(this.dataGridView1.CurrentRow.Cells["colCB"].Value))
                {

                    try
                    {
                        string strSql = "";
                        if (checkBox2.CheckState == CheckState.Checked)
                        {
                            strSql = "SELECT TB003 品号,TB012 品名,ISNULL(OPERATION_NAME,'') 工艺,TB013 规格,CEILING(TB004) AS 需领数量,CEILING(TB005+ISNULL(C.TE005,0)) AS 已领数量,(CASE WHEN (CEILING(TB004)-CEILING(TB005+ISNULL(C.TE005,0)))<CEILING(CASE WHEN TA015=0 THEN 0 ELSE TB004 / TA015 * " + dataGridView1.CurrentRow.Cells["数量"].Value + " END) THEN (CEILING(TB004)-CEILING(TB005+ISNULL(C.TE005,0))) ELSE CEILING(CASE WHEN TA015=0 THEN 0 ELSE TB004 / TA015 * " + dataGridView1.CurrentRow.Cells["数量"].Value + " END) END) AS 本次领料,TB009 仓库,A.MC002 仓库名称,CEILING(ISNULL(B.MC007,0)) 仓库库存,CEILING(MB064) 总库存,'' 领料工号,'' 领料人员,TB001 单别,TB002 单号,CEILING(ISNULL(D.TE005,0)) 待出库数量,MO.MO_ID MOID,MOCTB.MO_D_ID MODID,SOURCE_ID_RTK AS SOURCEIDRTK,SOURCE_ID_ROid AS SOURCEIDROid,MO.Owner_Org_ROid AS OwnerOrgROid,MOCTB.WAREHOUSE_ID FROM MOCTB LEFT JOIN MOCTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN INVMB ON TB003=MB001 LEFT JOIN CMSMC A ON A.MC001=TB009 LEFT JOIN INVMC B ON TB003=B.MC001 AND TB009=B.MC002 LEFT JOIN V_CLDL C ON TB001=C.TE011 AND TB002=C.TE012 AND TB003=C.TE004 LEFT JOIN V_DL D ON D.TE004=TB003 AND D.TE008=TB009 LEFT JOIN CMSMW ON TB006=MW001 LEFT JOIN ITEM ON ITEM_CODE=TB003 LEFT JOIN OPERATION ON TB006=OPERATION_CODE  LEFT JOIN MO ON TB001+'-'+TB002=MO.DOC_NO WHERE TB001='" + dataGridView1.CurrentRow.Cells["单别"].Value.ToString() + "' AND TB002='" + dataGridView1.CurrentRow.Cells["单号"].Value.ToString() + "' AND MB042<>'3' AND TB003 LIKE '" + textBox4.Text + "%' AND (TB009='" + textBox5.Text + "' OR '" + textBox5.Text + "'='') AND TB003 LIKE '" + textBox4.Text + "%' AND (TB006='" + dataGridView1.CurrentRow.Cells["工艺"].Value.ToString().Trim() + "' OR '" + dataGridView1.CurrentRow.Cells["工艺"].Value.ToString().Trim() + "'='****')";
                        
                        }
                        else
                        {
                            strSql = "SELECT TB003 品号,TB012 品名,ISNULL(OPERATION_NAME,'') 工艺,TB013 规格,CEILING(TB004) AS 需领数量,CEILING(TB005+ISNULL(C.TE005,0)) AS 已领数量,(CASE WHEN (CEILING(TB004)-CEILING(TB005+ISNULL(C.TE005,0)))<CEILING(CASE WHEN TA015=0 THEN 0 ELSE TB004 / TA015 * " + dataGridView1.CurrentRow.Cells["数量"].Value + " END) THEN (CEILING(TB004)-CEILING(TB005+ISNULL(C.TE005,0))) ELSE CEILING(CASE WHEN TA015=0 THEN 0 ELSE TB004 / TA015 * " + dataGridView1.CurrentRow.Cells["数量"].Value + " END) END) AS 本次领料,TB009 仓库,A.MC002 仓库名称,CEILING(ISNULL(B.MC007,0)) 仓库库存,CEILING(MB064) 总库存,'' 领料工号,'' 领料人员,TB001 单别,TB002 单号,CEILING(ISNULL(D.TE005,0)) 待出库数量,MO.MO_ID MOID,MOCTB.MO_D_ID MODID,SOURCE_ID_RTK AS SOURCEIDRTK,SOURCE_ID_ROid AS SOURCEIDROid,MO.Owner_Org_ROid AS OwnerOrgROid,MOCTB.WAREHOUSE_ID FROM MOCTB LEFT JOIN MOCTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN INVMB ON TB003=MB001 LEFT JOIN CMSMC A ON A.MC001=TB009 LEFT JOIN INVMC B ON TB003=B.MC001 AND TB009=B.MC002 LEFT JOIN V_CLDL C ON TB001=C.TE011 AND TB002=C.TE012 AND TB003=C.TE004 LEFT JOIN V_DL D ON D.TE004=TB003 AND D.TE008=TB009 LEFT JOIN CMSMW ON TB006=MW001 LEFT JOIN ITEM ON ITEM_CODE=TB003 LEFT JOIN OPERATION ON TB006=OPERATION_CODE  LEFT JOIN MO ON TB001+'-'+TB002=MO.DOC_NO WHERE  TB001='" + dataGridView1.CurrentRow.Cells["单别"].Value.ToString() + "' AND TB002='" + dataGridView1.CurrentRow.Cells["单号"].Value.ToString() + "' AND MB042<>'3' AND TB003 LIKE '" + textBox4.Text + "%' AND (TB009='" + textBox5.Text + "' OR '" + textBox5.Text + "'='') AND TB003 LIKE '" + textBox4.Text + "%' AND CEILING(TB004)-CEILING(TB005+ISNULL(C.TE005,0))>0 AND (TB006='" + dataGridView1.CurrentRow.Cells["工艺"].Value.ToString().Trim() + "' OR '" + dataGridView1.CurrentRow.Cells["工艺"].Value.ToString().Trim() + "'='****')";
                        
                        }
                        //textBox6.Text = strSql;
                        DataTable dt;
                  
                        db = new DataBase();
                        dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                        gridControl1.DataSource = dt;

                    }
                    catch
                    {

                    }
                }
                else
                {
                    //for (int i = 0; i <= this.dataGridView2.Rows.Count - 1; i++)
                    //{
                    //    string gddb = (this.dataGridView2.Rows[i].Cells["TB001"].Value == null) ? "" : this.dataGridView2.Rows[i].Cells["TB001"].Value.ToString();
                    //    string gddh = (this.dataGridView2.Rows[i].Cells["TB002"].Value == null) ? "" : this.dataGridView2.Rows[i].Cells["TB002"].Value.ToString();
                    //    if (gddb == dataGridView1.CurrentRow.Cells["单别"].Value.ToString() && gddh == dataGridView1.CurrentRow.Cells["单号"].Value.ToString())
                    //    {
                    //        this.dataGridView2.Rows.RemoveAt(i);
                    //        i = i - 1;
                    //    }
                    //}

                }
            }
        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void dataGridView2_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void dataGridView2_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            //this.dataGridView2.Controls.Clear();//移除所有控件 
            //if (e.ColumnIndex.Equals(this.dataGridView2.Columns["TB009"].Index))
            ////if (e.ColumnIndex == 13)
            ////判断单元格是否是"Company"列？    
            //{
            //    System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
            //    btn.Text = "...";//设置button文字    
            //    btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
            //    btn.Visible = true;//设置控件允许显示    

            //    btn.Width = this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex,
            //                    e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
            //    btn.Height = this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex,
            //                    e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

            //    btn.Click += new EventHandler(btn_Click);//为btn添加单击事件    

            //    this.dataGridView2.Controls.Add(btn);//dataGridView1中添加控件btn    

            //    btn.Location = new System.Drawing.Point(((this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
            //            (btn.Width)), this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
            //}
        }

        void btn_Click(object sender, EventArgs e)
        {
            string x1 = "仓库开窗";
            SCGY xx = new SCGY(x1);
            f = new SCKC();
            f.DataSelected += new EventHandler(f_DataSelected);
            f.cdgzzx += new SCKC.Cdgzzx(textBox2_TextChanged);
            f.Show(this);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        void f_DataSelected(object sender, EventArgs e)
        {

            //this.dataGridView2.CurrentCell.Value = f.UserId;
            //this.dataGridView2.Rows[this.dataGridView2.CurrentCell.RowIndex].Cells[this.dataGridView2.Columns["MC002"].Index].Value = f.UserName;

        }

        private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.ColumnIndex.Equals(this.dataGridView2.Columns["TB009"].Index))
            //{
            //    string strSql = null;
            //    strSql = "SELECT MC001,MC002 FROM CMSMC";
            //    try
            //    {
            //        sdr1 = db.GetDataReader1(strSql);

            //        while (sdr1.Read())
            //        {
            //            this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["MC002"].Index].Value = sdr1["MC002"].ToString();

            //        }

            //        if (sdr1.HasRows == false)
            //        {
            //            MessageBox.Show("该仓库不存在！", "软件提示");
            //            this.dataGridView2.CurrentCell.Value = "";
            //        }

            //    }

            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message, "软件提示");
            //        throw ex;
            //    }
            //    finally
            //    {
            //        sdr1.Close();
            //    }

            //}

            //if (e.ColumnIndex.Equals(this.dataGridView2.Columns["WLSL"].Index))
            //{
            //    double xlsl = Convert.ToDouble(this.dataGridView2.CurrentRow.Cells["TB004"].Value.ToString());
            //    double ylsl = Convert.ToDouble(this.dataGridView2.CurrentRow.Cells["TB005"].Value.ToString());
            //    double wlsl=xlsl-ylsl;
            //    double llsl = Convert.ToDouble(this.dataGridView2.CurrentCell.Value.ToString());
            //    if (llsl > xlsl - ylsl && checkBox2.CheckState == CheckState.Unchecked)
            //    {
            //        MessageBox.Show("不能超领！", "软件提示");
            //        this.dataGridView2.CurrentCell.Value = wlsl.ToString();
            //        return;
            //    }
            //}
        }

        private void textBox6_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button2.PerformClick();
            }
        }

        private void textBox3_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button2.PerformClick();
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.CheckState == CheckState.Unchecked)
            {
                textBox2.Text = "5610";
            }
            else if (checkBox2.CheckState == CheckState.Checked)
            {
                textBox2.Text = "5620";
            }
        }

        private void dataGridView1_Enter(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.dataGridView1.Controls.Clear();//移除所有控件 
                if (e.ColumnIndex.Equals(this.dataGridView1.Columns["员工编号"].Index))   
                {
                    System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
                    btn.Text = "...";//设置button文字    
                    btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
                    btn.Visible = true;//设置控件允许显示    

                    btn.Width = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
                                    e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
                    btn.Height = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
                                    e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

                    btn.Click += new EventHandler(btn1_Click);//为btn添加单击事件    

                    this.dataGridView1.Controls.Add(btn);//dataGridView1中添加控件btn    

                    btn.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
                            (btn.Width)), this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
                }
            }
            catch
            {

            }
        }

        void btn1_Click(object sender, EventArgs e)
        {
            //string x1 = dataGridView1["工作中心", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //SCGY xx = new SCGY(x1);
            g = new DDKC();
            g.kclx = "yuangong";
            g.DataSelected += new EventHandler(f1_DataSelected);
            //f.cdgzzx += new SCKC.Cdgzzx(textBox2_TextChanged);
            g.Show(this);
        }

        void f1_DataSelected(object sender, EventArgs e)
        {
            this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["员工编号"].Index].Value = g.UserId;
            this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["员工姓名"].Index].Value = g.UserName;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string strSql = "";
               strSql = "SELECT TB003 品号,TB012 品名,TB013 规格,CAST(TB004 AS REAL) AS 需领数量,CAST(TB005+ISNULL(C.TE005,0) AS REAL) AS 已领数量, CAST(TB004-TB005 AS REAL) AS 本次领料,TB009 仓库,A.MC002 仓库名称,CAST(ISNULL(B.MC007,0) AS REAL) 仓库库存,CAST(MB064 AS REAL) 总库存,ISNULL(MV001,'') 领料工号,ISNULL(MV002,'') 领料人员,TB001 单别,TB002 单号,TB006 工艺,CAST(ISNULL(D.TE005,0) AS REAL) 待出库数量,MO.MO_ID MOID,MO_D_ID MODID,SOURCE_ID_RTK AS SOURCEIDRTK,SOURCE_ID_ROid AS SOURCEIDROid,MO.Owner_Org_ROid AS OwnerOrgROid,MO_D.WAREHOUSE_ID FROM MOCTB LEFT JOIN MOCTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN INVMB ON TB003=MB001 LEFT JOIN CMSMC A ON A.MC001=TB009 LEFT JOIN INVMC B ON TB003=B.MC001 AND TB009=B.MC002 LEFT JOIN V_CLDL C ON TB001=C.TE011 AND TB002=C.TE012 AND TB003=C.TE004 LEFT JOIN V_DL D ON D.TE004=TB003 AND D.TE008=TB009 LEFT JOIN CMSMW ON TB006=MW001 LEFT JOIN ITEM ON ITEM_CODE=TB003 LEFT JOIN OPERATION ON TB006=OPERATION_CODE  LEFT JOIN MO ON TB001+'-'+TB002=MO.DOC_NO LEFT JOIN MO_D ON MO.MO_ID=MO_D.MO_ID AND ITEM.ITEM_BUSINESS_ID=MO_D.ITEM_ID AND ISNULL(OPERATION.OPERATION_ID,'00000000-0000-0000-0000-000000000000')=MO_D.OPERATION_ID LEFT JOIN V_CMSMV ON V_CMSMV.DOC_NO=RTRIM(TA001)+'-'+RTRIM(TA002) WHERE (TA011='2' OR TA011='3') AND MB042<>'2' AND TB012 LIKE '%" + textBox6.Text + "%' AND (TB009='" + textBox5.Text + "' OR '" + textBox5.Text + "'='') AND TB003 LIKE '" + textBox4.Text + "%' AND CAST((TB004-TB005) AS REAL)>0";
                DataTable dt;
                //textBox1.Text = strSql;
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                gridControl1.DataSource = dt;
            }
            catch
            {

            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string strSql = "";
            strSql = "SELECT TB003 品号,TB012 品名,TB013 规格,CAST(TB004 AS REAL) AS 需领数量,CAST(TB005+ISNULL(C.TE005,0) AS REAL) AS 已领数量, CAST(TB004-TB005 AS REAL) AS 本次领料,TB009 仓库,A.MC002 仓库名称,CAST(ISNULL(B.MC007,0) AS REAL) 仓库库存,CAST(MB064 AS REAL) 总库存,ISNULL(MV001,'') 领料工号,ISNULL(MV002,'') 领料人员,TB001 单别,TB002 单号,TB006 工艺,CAST(ISNULL(D.TE005,0) AS REAL) 待出库数量,MO.MO_ID MOID,MO_D_ID MODID,SOURCE_ID_RTK AS SOURCEIDRTK,SOURCE_ID_ROid AS SOURCEIDROid,MO.Owner_Org_ROid AS OwnerOrgROid,MO_D.WAREHOUSE_ID FROM MOCTB LEFT JOIN MOCTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN INVMB ON TB003=MB001 LEFT JOIN CMSMC A ON A.MC001=TB009 LEFT JOIN INVMC B ON TB003=B.MC001 AND TB009=B.MC002 LEFT JOIN V_CLDL C ON TB001=C.TE011 AND TB002=C.TE012 AND TB003=C.TE004 LEFT JOIN V_DL D ON D.TE004=TB003 AND D.TE008=TB009 LEFT JOIN CMSMW ON TB006=MW001 LEFT JOIN ITEM ON ITEM_CODE=TB003 LEFT JOIN OPERATION ON TB006=OPERATION_CODE  LEFT JOIN MO ON TB001+'-'+TB002=MO.DOC_NO LEFT JOIN MO_D ON MO.MO_ID=MO_D.MO_ID AND ITEM.ITEM_BUSINESS_ID=MO_D.ITEM_ID AND ISNULL(OPERATION.OPERATION_ID,'00000000-0000-0000-0000-000000000000')=MO_D.OPERATION_ID LEFT JOIN V_CMSMV ON V_CMSMV.DOC_NO=RTRIM(TA001)+'-'+RTRIM(TA002) WHERE (TA011='2' OR TA011='3') AND MB042<>'2' AND TB012 LIKE '%" + textBox7.Text + "%' AND (TB009='" + textBox8.Text + "' OR '" + textBox8.Text + "'='') AND TB003 LIKE '" + textBox9.Text + "%' AND CAST((TB004-TB005) AS REAL)>0";
            DataTable dt;
            //textBox1.Text = strSql;
            db = new DataBase();
            dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
            gridControl2.DataSource = dt;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl2.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DesignReport();
        }

        private void DesignReport()
        {
            if (FReportRunning)
                return;
            string thisFolder = Config.ApplicationFolder;
            DirectoryInfo theFolder = new DirectoryInfo(thisFolder + @"Reports\领料单\");
            string ReportName = theFolder + comboBox1.Text+".frx";
            FReport = new Report();
            FReport.Load(ReportName);
            RegisterData();
            FReport.Design();
            //PreviewReport();
        }

        private void RegisterData()
        {
            //FReport.RegisterData(FDataSet, "NorthWind");
            //FReport.RegisterData(FBusinessObject, "Categories BusinessObject");
        }

        private void PreviewReport()
        {
            if (FReportRunning)
                return;

            FReportRunning = true;
            try
            {
                RegisterData();
                FReport.Show();
            }
            finally
            {
                FReportRunning = false;
            }
        }

    }
}
        

    

